<?php


namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BaseController;
use App\Models\Banner;
use App\Models\Category;
use App\Models\Product;
use Illuminate\Http\Request;

class ProductController extends BaseController
{
    public $model = Product::class;

    public function pc_index() {
        $list_1 = Product::where('type', 1)->orderBy("id", "desc")->get();
        $list_2 = Product::where('type', 2)->orderBy("id", "desc")->get();
        $list_3 = Product::where('type', 3)->orderBy("id", "desc")->get();
        $list_4 = Product::where('type', 4)->orderBy("id", "desc")->get();
        $list_5 = Product::where('type', 5)->orderBy("id", "desc")->get();
        return $this->success(compact('list_1', 'list_2', 'list_3', 'list_4', 'list_5'));
    }

    public function pc_search() {
        $list_6 = Product::where('type', 6)->orderBy("id", "desc")->get();
        $list_7 = Product::where('type', 7)->orderBy("id", "desc")->get();
        $list_8 = Product::where('type', 8)->orderBy("id", "desc")->get();
        $list_9 = Product::where('type', 9)->orderBy("id", "desc")->get();
        $list_10 = Product::where('type', 10)->orderBy("id", "desc")->get();
        $list_11= Product::where('type', 11)->orderBy("id", "desc")->get();
        return $this->success(compact('list_6', 'list_7', 'list_8', 'list_9', 'list_10', 'list_11'));
    }

    public function pc_list() {
        $list = Category::with(['child' =>function($q) {
            $q->orderBy('sort', 'asc')->orderBy('id', 'desc');
        }, 'child.product'=> function($q) {
            $q->orderBy('sort', 'asc')->orderBy('id', 'desc');
        }])->where('parent_id', 0)->get();
        return $this->success($list);
    }


    /**
     * 列表
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request) {
        $input = $request->input();
        $search = ['category_id' => 0, 'parent_category_id' => 0];
        $list = Product::with('category')->with('parent_category')->apply($search, $input)
            ->orderBy('sort', 'asc')->orderBy("id", "desc")
            ->paginate(per_page());
        return $this->paginate($list);
    }

    /**
     * 详情
     * @param Request $request
     * @return mixed
     * @throws \Illuminate\Validation\ValidationException
     */
    public function info(Request $request) {
        $this->validate($request, [
            'id' => 'required|exists:products'
        ]);
        $id = \request("id");
        $info = Product::with('category')->with('parent_category')->find($id);
        return $this->success($info);
    }

    /**
     * 保存|修改
     * @param Request $request
     * @return mixed
     * @throws \Illuminate\Validation\ValidationException
     */
    public function store(Request $request) {
        $this->validate($request, [
            'file_path' => 'required',
            'category_id' => 'required',
            'id' => 'filled|exists:products'
        ]);
        $input = $request->input();
        $id = \request("id");
        $category_id = $request->input('category_id');
        $category = Category::find($category_id);
        if (!$category || $category->parent_id == 0)
            return $this->failed('只能选择二级分类');

        $input['parent_category_id'] = $category->parent_id;
        if ($id) {
            $banner = Product::where('id', $id)->first();
            $banner->update($input);
        } else {
            $info = Product::create($input);
            $id = $info->id;
        }
        return $this->success($id);
    }

}

